<template>
	<view class="body">
		
		<!-- 操作菜单 -->
		<paper-right-popup 
		:show="show"
		@hide="hidepopup"
		@addfriend="addfriend"
		@clear="clear">
		</paper-right-popup>
		
		<!-- 小纸条列表 -->
		<template v-if=" list.length > 0 ">
			<block v-for="(item,index) in list" :key="index">
				<paper-list :item="item" :index="index"></paper-list>
			</block>
		</template>
		<template v-else-if="!firstload">
			<view style="font-size: 50upx;font-weight: bold;color: #CCCCCC;
			padding-top: 100upx;" class="u-f-ajc">Loading ...</view>
		</template>
		<template v-else>
			<!-- 无内容默认 -->
			<no-thing></no-thing>
		</template>
		
	</view>
</template>

<script> 
	import paperList from "../../components/paper/paper-list.vue";
	import paperRightPopup from "../../components/paper/paper-right-popup.vue";
	import Time from "../../common/time.js";
	import noThing from "../../components/common/no-thing.vue";
	
	export default {
		components: {
			paperList,
			paperRightPopup,
			noThing
		},
		data() {
			return {
				firstload:false,
				show:false,
				list:[]
			}
		},
		// 监听下拉刷新
		onPullDownRefresh() {
			this.getdata();
		},
		// 监听导航栏按钮点击事件
		onNavigationBarButtonTap(e) {
			switch (e.index){
				case 0:
					this.User.navigate({
						url: '../user-list/user-list'
					});
					// uni.navigateTo({
					// 	url: '../user-list/user-list'
					// });
					this.hidepopup();
					break;
				case 1:
					this.showpopup();
					break;
			}
		},
		onShow() {
			// 获取列表
			this.getdata();
		},
		methods: {
			// 操作菜单
			addfriend(){
				console.log("添加趣友");
				this.hidepopup();
			},
			clear(){
				console.log("清除未读");
				this.hidepopup();
			},
			hidepopup(){
				this.show = false;
			},
			showpopup(){
				this.show = true;
			},
			// 获取数据
			getdata(){
				try{
					let userid = this.User.userinfo.id;
					if (!userid){
						this.firstload = true;
						return uni.stopPullDownRefresh();
					} 
					this.list = [];
					let list = uni.getStorageSync('chatlist'+this.User.userinfo.id);
					list = list ? JSON.parse(list) : [];
					for (var i = 0; i < list.length; i++) {
						list[i].time = Time.gettime.gettime(list[i].time);
					}
					this.list = list;
					this.firstload = true;
				}catch(e){
					return uni.stopPullDownRefresh();
				}
				uni.stopPullDownRefresh();
			}
		}
	}
</script>

<style scoped>
	.body{
		padding-left: 20upx;
		padding-right: 22upx;
	}
	
	
</style>
